package com.example.administrator.szb.http;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.example.administrator.szb.util.QTLog;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.UnsupportedEncodingException;


/**
 * Created by Administrator on 2017/9/10/010.
 * 自定义volley请求类型request 传入实体类型得到返回实体类
 */

public class GsonRequest<T> extends Request<T> {

    private final Response.Listener<T> mListener;

    private Gson mGson;

    private Class<T> mClass;

    private String special_url;

    public GsonRequest(int method, String url, Class<T> clazz, Response.Listener<T> listener,
                       Response.ErrorListener errorListener) {
        super(method, url, errorListener);
        special_url = url;

        /**
         * 格式化json数据中的时间
         */
        mGson =new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();
        mClass = clazz;
        mListener = listener;
    }

    public GsonRequest(String url, Class<T> clazz, Response.Listener<T> listener,
                       Response.ErrorListener errorListener) {
        this(Method.GET, url, clazz, listener, errorListener);
    }

    @Override
    protected Response<T> parseNetworkResponse(NetworkResponse response) {
        try {
            //String info = "{\"result\":0,\"err_code\":1,\"err_msg\":\"该手机号码已注册\",\"data\":{}}";
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));//原本的请求内容
            QTLog.e(jsonString);
            /**
             * 下面这个sb的字符串替换行为原因在于
             * 后台返回的json数据，当你请求机构详情的数据时候，如果这个机构有业务标签，那么business字段将是List类型，如果没有business字段将是：“”，也就是String,类型。
             * 所以你懂了吧，在于后台沟通无效的情况下，加上iso并没有问题，只能将business字段强行替换为空列表，不要觉得操蛋。
             * 生活本来如此。。。。
             */


            if (special_url.equals(URLAddress.URLBASE + URLAddress.URL_GWXQ)){
                String replace ="business\":\"\"";
               jsonString = jsonString.replaceAll(replace,"business\":[]");
            }
            else if(special_url.equals(URLAddress.URLBASE + URLAddress.URL_MY_ANSWER)){
                String replace = "\\{\\}";
                jsonString = jsonString.replaceAll(replace,"[]");
            }
            else if(special_url.equals(URLAddress.URLBASE + URLAddress.URL_GZMY)){
                String replace = "\\{\\}";
                jsonString = jsonString.replaceAll(replace,"[]");
            }

            else if (special_url.equals(URLAddress.URLBASE + URLAddress.URL_MESSAGE)){
                String replace = "\\{\\}";
                jsonString = jsonString.replaceAll(replace,"[]");
            }

            else if (special_url.equals(URLAddress.URLBASE + URLAddress.URL_MYGZ)){
                String replace = "\\{\\}";
                jsonString = jsonString.replaceAll(replace,"[]");
            }

            else if (special_url.equals(URLAddress.URLBASE + URLAddress.URL_TW_HD)){
                String replace = "\\{\\}";
                jsonString = jsonString.replaceAll(replace,"\"\"");
            }else if (special_url.equals(URLAddress.URLBASE + URLAddress.URL_WDGL_New)){
                String replace = "\"receive\":\\[";
                jsonString = jsonString.replaceAll(replace,"\"putForward\":[");

                String replace_two = "\"answer\":\\[";
                jsonString = jsonString.replaceAll(replace_two,"\"putForward\":[");
            }else if(special_url.equals(URLAddress.URLBASE + URLAddress.URL_YWBQGL)){
                String replace = "\\{\\}";
                jsonString = jsonString.replaceAll(replace,"[]");
            }else if (special_url.equals(URLAddress.URLBASE + URLAddress.URL_JGXG)){
                String replace = "\\{\\}";
                jsonString = jsonString.replaceAll(replace,"\"\"");
            }else if (special_url.equals(URLAddress.URLBASE + URLAddress.URL_ZXLB)){
                String replace = "\\{\\}";
                jsonString = jsonString.replaceAll(replace,"[]");
            }else if (special_url.equals(URLAddress.URLBASE + URLAddress.URL_GWJG)){
                String replace = "\"busArr\":\\{\\}";
                jsonString = jsonString.replaceAll(replace,"\"busArr\":[]");


                String replace_two = "\"dataList\":\\{\\}";
                jsonString = jsonString.replaceAll(replace_two,"\"dataList\":[]");
                String replace_one = "null";
                jsonString = jsonString.replaceAll(replace_one,"\"\"");
            }else if(special_url.equals(URLAddress.URLBASE + URLAddress.URL_XM)){
                String replace = "null";
                jsonString = jsonString.replaceAll(replace,"[]");

                String replace_resons = "\"reasons\":\\[\\]";
                jsonString = jsonString.replaceAll(replace_resons,"\"reasons\":\"\"");

                String replace_reson = "\"reason\":\\[\\]";
                jsonString = jsonString.replaceAll(replace_reson,"\"reason\":\"\"");

                String replace_indentInfo ="\"indentInfo\":\\[\\]";
                jsonString = jsonString.replaceAll(replace_indentInfo,"\"indentInfo\":\\{\\}");

                String replace_indentInfo_dh ="\"indentInfo\":\\{,";//有多余的逗号替换
                jsonString = jsonString.replaceAll(replace_indentInfo_dh,"\"indentInfo\":{");

                String pd = "\"\"";
                if (jsonString.indexOf(pd) != -1){
                    String replace_new = "\"\":\\{\"keyName\":\\[\\],\"valueName\":\\[\\]\\}";
                    jsonString = jsonString.replaceAll(replace_new,"");
                }
            }else if(special_url.equals(URLAddress.URLBASE + URLAddress.URL_DDXQ)){
                String pd = "\"\"";
                if (jsonString.indexOf(pd) != -1){
                    String replace_new = "\"\":\\{\"keyName\":null,\"valueName\":null\\}";
                    jsonString = jsonString.replaceAll(replace_new,"");
                }
            }

//            String replace = "";
//            String jsonString = jsonString_no.replaceAll(replace,""a":[]");

            try {
                mGson.fromJson(jsonString,mClass);
                return Response.success(mGson.fromJson(jsonString, mClass),
                        HttpHeaderParser.parseCacheHeaders(response));
            }catch (Exception e){
                e.toString();

            }

            return null;

        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        }
    }

    @Override
    protected void deliverResponse(T response) {
        mListener.onResponse(response);
    }

}